package org.kazao.sms;

import java.util.*;

public class KazaoOutgoingMessage extends KazaoMessage {
    private Date dispatchDate;
    
    public KazaoOutgoingMessage() {
        super(TYPE_OUTGOING, null, null, null, null, -1, false, false);
        setDispatchDate(null);
        setDate(new Date());
    }
    
    public KazaoOutgoingMessage(String recipient, String text) {
        super(TYPE_OUTGOING, new Date(), null, recipient, text, -1, false, false);
        setDispatchDate(null);
        setDate(new Date());
    }
    
    public KazaoOutgoingMessage(String recipient, String text, boolean flash, boolean report) {
        super(TYPE_OUTGOING, new Date(), null, recipient, text, -1, flash, report);
        setDispatchDate(null);
        setDate(new Date());
    }
    
    public void setRecipient(String recipient) {
        this.recipient = recipient;
    }
    
    public String getRecipient() {
        return recipient;
    }
    
    protected void setDispatchDate(Date date) {
        this.dispatchDate = date;
    }
    
    public Date getDispatchDate() {
        return dispatchDate;
    }
    
    protected String getPDU(String smscNumber) {
        String pdu;
        String str1, str2, str3;
        int i, high, low;
        char c;
        
        pdu = "";
        if ((smscNumber != null) && (smscNumber.length() != 0)) {
            str1 = "91" + toBCDFormat(smscNumber.substring(1));
            str2 = Integer.toHexString(str1.length() / 2);
            if (str2.length() != 2) str2 = "0" + str2;
            pdu = pdu + str2 + str1;
        } else if ((smscNumber != null) && (smscNumber.length() == 0)) {
            pdu = pdu + "00";
        }
        pdu = pdu + "11";
        pdu = pdu + "00";
        str1 = getRecipient();
        if( str1.charAt(0) == '+' ) {
            str1 = toBCDFormat(str1.substring(1));
            str2 = Integer.toHexString(getRecipient().length() - 1);
            str1 = "91" + str1;
        } else {
            str1 = toBCDFormat(str1);
            str2 = Integer.toHexString(getRecipient().length());
            str1 = "81" + str1;
        }
        if (str2.length() != 2) {
            str2 = "0" + str2;
        }
        
        pdu = pdu + str2 + str1;
        pdu = pdu + "00";
        switch (getMessageEncoding()) {
            case MESSAGE_ENCODING_7BIT:
                pdu = pdu + "00";
                break;
            case MESSAGE_ENCODING_8BIT:
                pdu = pdu + "04";
                break;
            case MESSAGE_ENCODING_UNICODE:
                pdu = pdu + "08";
                break;
        }
        pdu = pdu + "AA";
        switch (getMessageEncoding()) {
            case MESSAGE_ENCODING_7BIT:
//                if (flash) {
//                    str1 = Integer.toHexString(getText().length()+2);
//                    if (str1.length() != 2) {
//                        str1 = "0" + str1;
//                    }
//                    str2 = textToPDU(getText());
//                    pdu = pdu + str1 + "0001" + str2;
//                } else {
                str1 = Integer.toHexString(getText().length());
                if (str1.length() != 2) {
                    str1 = "0" + str1;
                }
                str2 = textToPDU(getText());
                pdu = pdu + str1 + str2;
//                }
                break;
            case MESSAGE_ENCODING_8BIT:
                str1 = getText();
                str2 = "";
                for (i = 0; i < str1.length(); i ++) {
                    c = str1.charAt(i);
                    str2 = str2 + ((Integer.toHexString((int) c).length() < 2) ? "0" + Integer.toHexString((int) c) : Integer.toHexString((int) c));
                }
//                if (flash) {
//                    str1 = Integer.toHexString(getText().length()+2);
//                    if (str1.length() != 2) {
//                        str1 = "0" + str1;
//                    }
//                    pdu = pdu + str1 + "0001" + str2;
//                } else {
                str1 = Integer.toHexString(getText().length());
                if (str1.length() != 2) {
                    str1 = "0" + str1;
                }
                pdu = pdu + str1 + str2;
//                }
                break;
            case MESSAGE_ENCODING_UNICODE:
                str1 = getText();
                str2 = "";
                for (i = 0; i < str1.length(); i ++) {
                    c = str1.charAt(i);
                    high = (int) (c / 256);
                    low = c % 256;
                    str2 = str2 + ((Integer.toHexString(high).length() < 2) ? "0" + Integer.toHexString(high) : Integer.toHexString(high));
                    str2 = str2 + ((Integer.toHexString(low).length() < 2) ? "0" + Integer.toHexString(low) : Integer.toHexString(low));
                }
                if (flash) {
                    str1 = Integer.toHexString(getText().length() * 2);
                    if (str1.length() != 2) {
                        str1 = "0" + str1;
                    }
                    pdu = pdu + str1 + "0001" + str2;
                } else {
                    str1 = Integer.toHexString(getText().length() * 2);
                    if (str1.length() != 2) {
                        str1 = "0" + str1;
                    }
                    pdu = pdu + str1 + str2;
                }
                break;
        }
        return pdu.toUpperCase();
    }
    
    private String textToPDU(String text) {
        String pdu, str1;
        byte[] oldBytes, newBytes;
        BitSet bitSet;
        int i, j, k, value1, value2;
        
        str1 = "";
        text = KazaoGSMAlphabets.text2Hex(text, KazaoGSMAlphabets.GSM7BITDEFAULT);
        for (i = 0; i < text.length(); i += 2) {
            j = (Integer.parseInt("" + text.charAt(i), 16) * 16) + Integer.parseInt("" + text.charAt(i + 1), 16);
            str1 += (char) j;
        }
        text = str1;
        oldBytes = text.getBytes();
        bitSet = new BitSet(text.length() * 8);
        
        value1 = 0;
        for (i = 0; i < text.length(); i ++) {
            for (j = 0; j < 7; j ++) {
                value1 = (i * 7) + j;
                if ((oldBytes[i] & (1 << j)) != 0) {
                    bitSet.set(value1);
                }
            }
        }
        value1 ++;
        
        if (((value1 / 56) * 56) != value1) {
            value2 = (value1 / 8) + 1;
        } else {
            value2 = (value1 / 8);
        }
        if (value2 == 0) {
            value2 = 1;
        }
        
        newBytes = new byte[value2];
        for (i = 0; i < value2; i ++) {
            for (j = 0; j < 8; j ++) {
                if ((value1 + 1) > ((i * 8) + j)) {
                    if (bitSet.get(i * 8 + j)) {
                        newBytes[i] |= (byte) (1 << j);
                    }
                }
            }
        }
        pdu = "";
        for (i = 0; i < value2; i ++) {
            str1 = Integer.toHexString((int) newBytes[i]);
            if (str1.length() != 2) {
                str1 = "0" + str1;
            }
            str1 = str1.substring(str1.length() - 2, str1.length());
            pdu += str1;
        }
        return pdu;
    }
    
    private String toBCDFormat(String s) {
        String bcd;
        int i;
        
        if ((s.length() % 2) != 0) {
            s = s + "F";
        }
        bcd = "";
        for (i = 0; i < s.length(); i += 2) {
            bcd = bcd + s.charAt(i + 1) + s.charAt(i);
        }
        return bcd;
    }
}
